/*
 * Copyright (c) 2011 Vasile Vilvoiu. All rights reserved.
 *   
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * fett_mail_server.c - Server executable entry point
 */

/* other includes */
#include <signal.h>
#include "ftypes.h"
#include "ferror.h"
#include "config.h"
#include "cmgr.h"

/* defines */
#define ERROR_LOG_FILE	"fett-mail-server.log"
#define CONFIG_FILE		"fett-mail-server.cfg"

#define RET_OK			0
#define RET_LOG_FAIL	-1
#define RET_CFG_FAIL	-2
#define RET_NET_FAIL	-3

/* terminate flag; when nonzero, deinitialise application */
static bool server_terminate = false;

/*
 * sigterm_handler - SIGTERM signal handler
 */
static void sigterm_handler(int param) {
	server_terminate = true;
}

/*
 * create_default_listeners - create POP3 and SMTP listeners
 */
int create_default_listeners() {

	int err = 0;
	CMGR_CONNECTION *pop3_connection = NULL;
	CMGR_CONNECTION *smtp_connection = NULL;

	/* create pop3 listener */
	err = cmgr_create_listener(cfg_pop3_listen_port, NULL, &pop3_connection);
	if (err != ERR_NONE) {
		return err;
	}
	err = cmgr_register_connection(pop3_connection);
	if (err != ERR_NONE) {
		return err;
	}

	/* create smtp listener */
	/*err = cmgr_create_listener(cfg_smtp_listen_port, NULL, &smtp_connection);
	if (err != ERR_NONE) {
		return err;
	}
	err = cmgr_register_connection(smtp_connection);
	if (err != ERR_NONE) {
		return err;
	}*/

	/* all ok */
	return ERR_NONE;
}

/*
 * main - application entry point
 */
int main(int argc, char* argv[]) {

	/* initialise error log */
	if (err_init(ERROR_LOG_FILE, true) != ERR_NONE) {
		printf("ERROR: could not initialise error log... well this sucks :(\n");
		return RET_LOG_FAIL;
	}

	/* read configuration */
	if (cfg_init(CONFIG_FILE) != ERR_NONE) {
		printf("ERROR: could not read/parse config file; check error log\n");
		return RET_CFG_FAIL;
	}

	/* initialise connection manager */
	if (cmgr_init() != ERR_NONE) {
		printf("ERROR: could not initialise connection manager; check error log\n");
		return RET_NET_FAIL;
	}

	/* create POP3 and SMTP connection listeners */
	if (create_default_listeners() != ERR_NONE) {
		printf("ERROR: could not initialise SMTP/POP connection; check error log\n");
		return RET_NET_FAIL;
	}

	/* set signal handlers */
	signal(SIGTERM, sigterm_handler);

	/* main loop */
	while (!server_terminate) {
		cmgr_handle_connections();
	}

	/* deinitialise all */
	cmgr_kill();
	cfg_kill();
	err_kill();

	/* all ok - return 0 */
	err_error(ERR_NONE, "server shutdown successful. bye!", __HERE__);
	return RET_OK;
}
